package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.TehGrade;
import com.neuedu.studentmanage.service.TehGradeService;

@Controller
public class TehGradeController {

	@Autowired
	private TehGradeService tehGradeService;

	@RequestMapping("tehgrade/index")
	public String doIndex() {
		return "admin/tehGradeList";
	}

	@PostMapping("admin/tehGradeList")
	@ResponseBody
	public Pager<TehGrade> doList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<TehGrade> pager = new Pager<>(page, rows);

		List<TehGrade> list = tehGradeService.findListByPager(pager);
		Integer total = tehGradeService.findTotalByPager(pager);

		pager.setRows(list);
		pager.setTotal(total);

		return pager;
	}

	@PostMapping("tehGrade/listall")
	@ResponseBody
	public List<TehGrade> doList2() {
		List<TehGrade> tehGradeList = tehGradeService.findAll();
		return tehGradeList;
	}

	@RequestMapping("tehGrade/add")
	@ResponseBody
	public Result doAdd(TehGrade tehGrade) {
		Result result = tehGradeService.add(tehGrade);
		return result;
	}

	@RequestMapping("tehGrade/edit")
	@ResponseBody
	public Result doEdit(TehGrade tehGrade) {
		Result result = tehGradeService.edit(tehGrade);
		return result;
	}

	@RequestMapping("tehGrade/delete")
	@ResponseBody
	public Result doDelete(Integer tgId) {
		List<TehGrade> list = new ArrayList<TehGrade>();
		list.add(new TehGrade(tgId, null));

		Result result = tehGradeService.deleteByIds(list);
		return result;
	}
}
